const Base = require('./base.js');
const path = require('path');
const fs = require('fs');

module.exports = class extends Base {
  async indexAction() {
    let userInfo = await this.session('user_info')
    if (!userInfo) return this.fail(-1, '未授权请求')
    let file = this.file('file')
    if (!file) return this.fail(-1, '参数错误')
    let nowDate = think.datetime(new Date(), 'YYYYMMDD')
    let uploadPath = path.join(think.ROOT_PATH, 'www/static/uploads', nowDate);
    let fileName = think.uuid('v1') + path.extname(file.path)
    think.mkdir(uploadPath)
    fs.renameSync(file.path, path.join(uploadPath, fileName))
    this.json({
        status: 200, message: 'success',
        data: {
          url: '/static/uploads/' + nowDate + '/' + fileName
        }
    })
  }
  async allAction() {
    return this.display();
  }
};
